#! /usr/bin/env node

const program = require("commander")
const core = require("./tts-core")
const log = require('./log')
const store = require('./store')


process.env.clientId = "a4a6a245868a468d9de5ca33964e2814"
process.env.clientSecret = "3ba9f054590b4b0590e9cf1086134c6c"


//全局捕获异常
process.on('uncaughtException', err => {
    log.debug(err)
    log.error(err.message)
})


program.version(require("./package.json").version)
program.option("-D, --debug", "debug输出", function () {
    process.env.debug = true
})


//设置clientId和secret
program.command("set <clientId> <clientSecret>")
    .description("设置clientId和secret")
    .action((clientId, clientSecret, cmdObj) => {
        store.storeClientIdAndSecret({clientId, clientSecret})
    });

//语音合成转换
program.command('parse [text]')
    .description("语音合成")
    .alias("p")
    .option("-f, --file <p>", "source文件")
    .option("-d, --destDir <p>", "目标存放目录")
    .action((text, cmdObj) => {
        if (!text && !cmdObj.file) {
            log.error("missing source text or file!")
            return
        }
        if (text) {
            core.tts(text, undefined, undefined, cmdObj.destDir)
            return
        }
        if (cmdObj.file) {
            core.ttsFromF(cmdObj.file, cmdObj.destDir)
        }
    })

program.parse(process.argv)

